package pw.yumc.Residence.commandmain;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.chat.ChatChannel;

public class CommandRc extends BaseCommand {
    ResidenceMain plugin;

    public CommandRc(final ResidenceMain plugin) {
        super("rc");
        this.plugin = plugin;
        setOnlyPlayerExecutable();
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final String pname = sender.getName();
        if (plugin.getConfigManager().chatEnabled()) {
            if (args.length == 0) {
                plugin.getPlayerListener().tooglePlayerResidenceChat((Player) sender);
            } else {
                final String area = plugin.getPlayerListener().getCurrentResidenceName(pname);
                if (area != null) {
                    final ChatChannel channel = plugin.getChatManager().getChannel(area);
                    if (channel != null) {
                        String message = "";
                        for (final String arg : args) {
                            message = message + " " + arg;
                        }
                        channel.chat(pname, message);
                    } else {
                        sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidChannel"));
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NotInResidence"));
                }
            }
        } else {
            sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ChatDisabled"));
        }
    }

}
